package pro.cherish.knowledgedemoioc.controller;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import pro.cherish.knowledgedemoioc.service.UserService;


import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-09-28 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：Tcontroller2
 * @Description 功能说明：使用内部类形式的线程实现类，实现在线程中获取spring 容器中的bean
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-09-28 cherish
 */
@RestController
public class Tcontroller2 {

    @Resource
    private UserService userService;

    @GetMapping("/t2")
    public void t1(String name, Integer age) throws ExecutionException, InterruptedException {
        System.out.println(name + " " + age);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 4, 200, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        Future future = executor.submit(new helloThreadHandler("cherish"));
        System.out.println(future.get());

    }

    @Data
    @NoArgsConstructor
    public class helloThreadHandler implements Callable {
        private String name;

        public helloThreadHandler(String name) {
            this.name = name;
        }

        @Override
        public Object call() throws Exception {
            Object user = userService.getUser();
            System.out.println(user);
            System.out.println("线程" + Thread.currentThread().getName() + "调用了，获取到了name" + name);
            return "我知道了";
        }
    }
}
